<?php
include_once($_SERVER['DOCUMENT_ROOT']."/config.php");
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/db.php');
class written_language_name_translation
{
	private $db;
	private $table_name = 'written_language_name_translation';
	function __construct()
	{
		$this->db = new db();
	}
	public function insert($data)
	{
		//todo
		/*
		if(!isset($data['film_id'])||empty($data['film_id']))
		{
			return false;
		}
		$data_for_query['film_id'] = intval($data['film_id']);
		if(isset($data['CN'])&&!empty($data['CN']))
		{
			$data_for_query['CN'] = intval($data['CN']);
		}
		if(isset($data['HK'])&&!empty($data['HK']))
		{
			$data_for_query['HK'] = intval($data['HK']);
		}
		if(isset($data['TW'])&&!empty($data['TW']))
		{
			$data_for_query['TW'] = intval($data['TW']);
		}
		if(isset($data['US'])&&!empty($data['US']))
		{
			$data_for_query['US'] = intval($data['US']);
		}
		$sql = "INSERT INTO `".$this->table_name."` (".$this->db->implodeToColumn(array_keys($data_for_query)).") VALUES (".$this->db->implodeToValues(array_values($data_for_query)).")";
		$count = $this->db->query($sql);
		if(empty($count))
		{
			throw new Exception("INSERT INTO `".$this->table_name."` faild");
		}
		else
		{
			return $count;
		}*/
	}

	public function select($where = '1',$column='*')
	{
		$sql_1 = "SELECT ".$this->db->implodeToColumn($column)." FROM ".$this->table_name." WHERE ".$this->db->implodeToWhere($where);
		$result_1 = $this->db->query($sql_1);
		$result_1->setFetchMode(PDO::FETCH_ASSOC);
		$fetch_result = $result_1->fetchAll();
		if($fetch_result==false)
		{
			throw new Exception("faild");
		}
		else
		{
			return $fetch_result;
		}
	}
	
	public function getIdAndName()
	{
		$fetch_result = $this->select();
		foreach($fetch_result as $row)
		{
			$id_name[$row['written_language_id']] = $row;
		}

		if(empty($id_name))
		{
			//throw new Exception("getIdAndName faild");
			throw new Exception("");
		}
		else
		{
			return $id_name;
		}
	}

	public function getIdAndNameByTag($tag=array('zh-Hans-CN','en-US'))
	{
		$sql_1 = "SELECT `written_language_id`,".$this->db->implodeToColumn($tag)." FROM `written_language_name_translation`";
		$result_1 = $this->db->query($sql_1);
		$result_1->setFetchMode(PDO::FETCH_ASSOC);
		$fetch_result = $result_1->fetchAll();
		if(is_array($tag))
		{
			foreach($fetch_result as $row)
			{
				$id_name[$row['written_language_id']] = $row;
			}
		}
		else
		{
			foreach($fetch_result as $row)
			{
				$id_name[$row['written_language_id']] = $row[$tag];
			}
		}
		if(empty($id_name))
		{
			//throw new Exception("getIdAndName faild");
			throw new Exception("");
		}
		else
		{
			return $id_name;
		}
	}
}

?>
